<template>
  <!--五级分类回款报表-->
  <section class="page five-level-return-rate-report">
    <data-form :submitLoading="submitLoading" :model="searchModel" @onSearch="refreshData">
      <template slot="inputs">
        <el-form-item label="时间" prop="caseFollowInTime">
          <el-date-picker type="daterange" :editable="false" v-model="searchModel.caseFollowInTime" placeholder="选择日期时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="五级分类" prop="styleState">
          <el-select placeholder="请选择" v-model="searchModel.styleState" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('StyleState')" :key="index" :label="name" :value="code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select placeholder="请选择" v-model="searchModel.sysCode" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('SysCode')" :key="index" :label="name" :value="code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="放款渠道" prop="loanChannel">
          <el-select placeholder="请选择" v-model="searchModel.loanChannel" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('LoanChannel')" :key="index" :label="name" :value="code">
            </el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="dataSet">
      <template slot="buttons">
        <el-button @click="exportCaseInfo" v-auth="2009">导出</el-button>
      </template>
      <template slot="columns">
          <el-table-column label="五级分类" prop="styleState" :formatter="(row) => $filter.encryptStyleState(row.styleState)" show-overflow-tooltip></el-table-column>
          <el-table-column label="客户数" prop="personalNum"></el-table-column>
          <el-table-column label="贷款余额(元)" prop="loanAmount"></el-table-column>
          <el-table-column label="余额占比" prop="balanceRate">
            <template slot-scope="scope">
              <span>{{scope.row.balanceRate!== 'null'?$filter.toPercent(scope.row.balanceRate):""}}</span>
            </template>
          </el-table-column>
          <el-table-column label="催收回款率" prop="returnRate">
            <template slot-scope="scope">
              <span>{{scope.row.returnRate!== 'null'?$filter.toPercent(scope.row.returnRate):""}}</span>
            </template>
          </el-table-column>
      </template>
    </data-box>
  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Auth, Layout, Dependencies } from '~/core/decorator';
import DataBox from '~/components/common/data-box.vue';
import DataForm from "~/components/common/data-form.vue"
import { ReportManageService } from "~/services/domain-service/report-manage.service";
import { switchMap } from 'rxjs/operator/switchMap';

// @Auth(1004)
@Layout('workspace')
@Component({
  components: {
    DataBox,
    DataForm
  }
})
export default class FiveLevelReturnRateReport extends Vue {
  @Dependencies(ReportManageService) private reportManageService: ReportManageService;
  private searchModel: any = {
    caseFollowInTime:'',
    styleState:'',
    sysCode:'',
    loanChannel:''
  }
  private dataSet: any = []
  private submitLoading: any = true

  //案件导出
  private exportCaseInfo() {
    if(this.dataSet.length === 0){
      this.$message("暂无数据可以导出")
      return
    }
    const params: any = {
      styleState: this.searchModel.styleState,
      sysCode: this.searchModel.sysCode,
      loanChannel: this.searchModel.loanChannel,
      caseFollowInTimeStart: this.$filter.dateRanageFormat(this.searchModel.caseFollowInTime).start,
      caseFollowInTimeEnd: this.$filter.dateRanageFormat(this.searchModel.caseFollowInTime).end
    }
    this.reportManageService
      .exportFiveLevelData(params)
      .subscribe(data => this.$common.downloadFile(data.url));
  }
  
  private refreshData() {
    const params: any = {
      styleState: this.searchModel.styleState,
      sysCode: this.searchModel.sysCode,
      loanChannel: this.searchModel.loanChannel,
      caseFollowInTimeStart: this.$filter.dateRanageFormat(this.searchModel.caseFollowInTime).start,
      caseFollowInTimeEnd: this.$filter.dateRanageFormat(this.searchModel.caseFollowInTime).end
    }
    this.reportManageService.queryFiveLevelData(params)
      .subscribe(data => {
        this.dataSet = data
        this.submitLoading = false
      })
  }

  private mounted() {
    this.refreshData()
  }

}
</script>

<style lang="less">

</style>
